<template>
  <el-container>
    <el-header>
      <div>
        <span>出版社管理系统</span>
      </div>
      <el-button type="info" @click="logout">退出</el-button>
    </el-header>
    <el-container>
      <el-aside :width="isCollapse ? '64px' : '270px'">
        <div class="toggle-button" @click="toggleCollapse">
          <i :class="isCollapse ? 'el-icon-s-unfold' : 'el-icon-s-fold'"></i>
        </div>
        <el-menu
          background-color="#367bc0"
          text-color="#fff"
          active-text-color="#2d2d2d"
          unique-opened
          :collapse="isCollapse"
          :collapse-transition="false"
          router
          :default-active="activePath"
        >
          <el-submenu index="1">
            <template slot="title">
              <i class="el-icon-menu"></i>
              <span>客户管理</span>
            </template>
            <el-menu-item index="clientInfo" @click="saveNavState">
              <template slot="title">
                <i class="el-icon-user-solid"></i>
                <span>客户信息</span>
              </template>
            </el-menu-item>
            <el-menu-item index="clientClass" @click="saveNavState">
              <template slot="title">
                <i class="el-icon-s-grid"></i>
                <span>客户类别</span>
              </template>
            </el-menu-item>
            <el-submenu index="1-1">
              <template slot="title">
                <i class="el-icon-s-claim"></i>
                <span>客户活动</span>
              </template>
              <el-menu-item index="clientActiveBook" @click="saveNavState">
                <template slot="title">
                  <i class="el-icon-collection"></i>
                  <span>增样书</span>
                </template>
              </el-menu-item>
              <el-menu-item index="clientActiveEmail" @click="saveNavState">
                <template slot="title">
                  <i class="el-icon-message"></i>
                  <span>慰问信</span>
                </template>
              </el-menu-item>
              <el-menu-item index="clientActiveGift" @click="saveNavState">
                <template slot="title">
                  <i class="el-icon-present"></i>
                  <span>慰问礼</span>
                </template>
              </el-menu-item>
            </el-submenu>
          </el-submenu>
          <el-submenu index="2">
            <template slot="title">
              <i class="el-icon-menu"></i>
              <span>作者管理</span>
            </template>
            <el-menu-item index="authorInfo" @click="saveNavState">
              <template slot="title">
                <i class="el-icon-user-solid"></i>
                <span>作者信息</span>
              </template>
            </el-menu-item>
            <el-menu-item index="authorClass" @click="saveNavState">
              <template slot="title">
                <i class="el-icon-s-grid"></i>
                <span>作者类别</span>
              </template>
            </el-menu-item>
            <el-submenu index="2-1">
              <template slot="title">
                <i class="el-icon-s-claim"></i>
                <span>作者事务</span>
              </template>
              <el-menu-item index="authorBook" @click="saveNavState">
                <template slot="title">
                  <i class="el-icon-chat-dot-square"></i>
                  <span>座谈会</span>
                </template>
              </el-menu-item>
              <el-menu-item index="authorInvite" @click="saveNavState">
                <template slot="title">
                  <i class="el-icon-collection"></i>
                  <span>出书邀约</span>
                </template>
              </el-menu-item>
              <el-menu-item index="authorTalk" @click="saveNavState">
                <template slot="title">
                  <i class="el-icon-s-ticket"></i>
                  <span>慰问礼</span>
                </template>
              </el-menu-item>
            </el-submenu>
          </el-submenu>
        </el-menu>
      </el-aside>
      <el-main>
        <router-view></router-view>
      </el-main>
    </el-container>
  </el-container>
</template>

<script>
//消息订阅与发布
import pubsub from "pubsub-js";

export default {
  name: "home",
  data() {
    return {
      isCollapse: false,
      activePath: "",
    };
  },
  methods: {
    //退出登录
    logout() {
      sessionStorage.clear();
      this.$router.push("/login");
    },
    //侧边栏的收缩
    toggleCollapse() {
      this.isCollapse = !this.isCollapse;
    },
    //保存链接的激活状态
    saveNavState(VueComponent) {
      sessionStorage.setItem("activePath", VueComponent.index);
      this.activePath = VueComponent.index;
    },
  },
  created() {
    this.activePath = sessionStorage.getItem("activePath");
  },
  mounted() {
    pubsub.subscribe("Reload", (_, data) => {
      if (data === "reload")
        this.activePath = sessionStorage.getItem("activePath");
    });
  },
};
</script>

<style scoped>
.el-container {
  height: 100%;
}

.el-header {
  background-color: rgb(40, 74, 119);
  display: flex;
  justify-content: space-between;
  align-items: center;
  color: rgb(173, 173, 173);
  font-weight: 600;
  font-size: 20px;
  letter-spacing: 5px;
}

.el-header > div {
  display: flex;
  align-items: center;
}

.el-header > div > span {
  margin-left: 15px;
}

.el-aside {
  background-color: #367bc0;
}

.el-aside .el-menu {
  border-right: none;
}

.el-main {
  background-color: rgb(255, 255, 255);
}

.toggle-button {
  font-size: 10px;
  line-height: 30px;
  color: #fff;
  text-align: center;
  letter-spacing: 0.2em;
  cursor: pointer;
}
</style>
